package net.neptech.handler;


import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import net.neptech.exception.BusinessException;
import net.neptech.vm.ResponseResult;
import net.neptech.vm.enums.ResultStatusEnum;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

/**
 * 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    public ResponseResult<?> businessExceptionHandler(BusinessException e) {
        log.error("BusinessException: {}", e.getMessage(), e);
        return ResponseResult.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseResult<?> runtimeExceptionHandler(RuntimeException e) {
        // 处理Seata包装的异常
        if (e.getCause() instanceof BusinessException businessEx) {
            log.error("BusinessException(wrapped by Seata): {}", businessEx.getMessage(), businessEx);
            return ResponseResult.fail(businessEx.getCode(), businessEx.getMessage());
        }
        log.error("RuntimeException: {}", e.getMessage(), e);
        return ResponseResult.fail(ResultStatusEnum.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
        log.error("MethodArgumentNotValidException: {}", message);
        return ResponseResult.fail(ResultStatusEnum.BAD_REQUEST.getCode(), message);
    }

    @ExceptionHandler(NotLoginException.class)
    public Object handleNotLoginException(NotLoginException e) {
        log.error("handleNotLoginException：{}", e.getMessage());
        return ResponseResult.fail(ResultStatusEnum.UNAUTHORIZED.getCode(), "用户未登录或登录已过期，请重新登录");
    }

    /**
     * 权限码异常
     */
    @ExceptionHandler(NotPermissionException.class)
    public Object handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
        return ResponseResult.fail(ResultStatusEnum.UNAUTHORIZED, "没有访问权限，请联系管理员授权");
    }

    /**
     * 角色权限异常
     */
    @ExceptionHandler(NotRoleException.class)
    public Object handleNotRoleException(NotRoleException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
        return ResponseResult.fail(ResultStatusEnum.UNAUTHORIZED, "没有访问权限，请联系管理员授权");
    }


}
